<?php
/**
 *   Joomdiet - components/views/recommendedingestsresult/view.html.php
 *   Author: Juan Dapena Paz (juan@bittingbits.com)
 *   Copyright (C) 2010 Bitting Bits S.L.
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
defined('_JEXEC') or die('Restricted access');
jimport('joomla.application.component.view');

class JoomdietViewRecommendedIngestsResult extends JView
{
    function display($tpl=null)
    {
        $menu = JoomdietHelperFrontMenu::getInstance();
        echo $menu->display();
        
        // adding base script
        $document =& JFactory::getDocument();
        $document->addScript('components'.DS.'com_joomdiet'.DS.'assets'.DS.'javascript'.DS.'recommendedresult.js');
        
        $model = $this->getModel();
        
        $variables = $model->getData();
        
        // calculating population group
        $age = JRequest::getVar('agefield');
        if ($age <= 8 and $age >= 4) {
            $population_group = 2;
        } else if ($age <= 3) {
            $population_group = 1;
        } else {
            // man or woman
            $gender = JRequest::getVar('genderfield');
            if ($gender == 'male') $population_group = 3;
            else {
                $status = JRequest::getVar('statusfield');
                if ($status == 'none') $population_group = 4;
                else if ($status == 'gestationone' 
                      or $status == 'gestationtwo') $population_group = 6;
                else $population_group = 5;
            }
        }
        
        // physical activity
        $pa = JRequest::getVar('physicalactivity');
        
        $result = array();
        foreach ($variables as $variable) {
            $encapsule_array = array();
            $encapsule_array['0'] = $variable;
            $encapsule_array['1'] = 
                $model->getRecommended($variable->id, $age, $pa, $population_group);
                
            $result[] = $encapsule_array;
        }
        
        $this->assignRef('result', $result);
        
        parent::display();
    }
}
?>
